jQuery(function($) {

    $(document).ready(function() {
        $('input[type="text"]').setMask();
        appendAlts();
        checkSelected("pessoa");
        checkSelected("tipo");
    });

    $("input[name='pessoa']").off("click").on("click", function() {
        checkSelected(this.name);
        checkSelected("tipo");
    });
    $("input[name='tipo']").off("click").on("click", function() {
        clearFields($("div[alt='revendedor_data']").find("input"));
        checkSelected(this.name);
    });

});

    function appendAlts() {
        $(".form-row.field-nomefantasia.field-cnpj.field-inscricaoestadual").attr("alt", "pj_data");
        $(".form-row.field-cpf.field-rg").attr("alt", "pf_data");
        $(".form-row.field-comissao.field-tabelaprecovenda.field-limitecompra").attr("alt", "revendedor_data");
    }

    function clearFields(inputs) {
        for (var i = 0; i < inputs.length; i++) {
            inputs[i].value = "";
        }
    }

    function checkSelected(name) {
        $("div[alt='revendedor_data']").hide();
        var objects = document.getElementsByName(name);
        for (var i = 0; i < objects.length; i++) {
            if (objects[i].checked) {
                if ($.trim(objects[i].parentNode.textContent) == "Pessoa Física") {
                    $("div[alt='pf_data']").show();
                    $("div[alt='pj_data']").hide();
                    clearFields($("div[alt='pj_data']").find("input"));
                } else if ($.trim(objects[i].parentNode.textContent) == "Pessoa Jurídica") {
                    $("div[alt='pj_data']").show();
                    $("div[alt='pf_data']").hide();
                    clearFields($("div[alt='pf_data']").find("input"));
                } else if ($.trim(objects[i].parentNode.textContent) == "Revendedor") {
                    $("div[alt='revendedor_data']").show();
                }

            }
        }
    }

/*

    updateValorSugerido("td.valorunitario input", "td.quantidade input", "td.valortotal");
    addLiveEvent(".ui-menu-item a", "input.ui-autocomplete-input", "input.ui-autocomplete-input");


function updateValorSugerido(inputField1, inputField2, output) {
    $(inputField1).live("focusout", function() {
        refreshValues(inputField1, inputField2, output);
    });
    $(inputField2).live("focusout", function() {
        refreshValues(inputField1, inputField2, output);
    });
}

function addLiveEvent(selectorClick, selectorKeypress, target) {
    $(selectorKeypress).live("keypress", function(event) {
        if (event.which == 13) {
            $(target).hide().next().hide();
        }
    });

    $(selectorClick).live("click", function() {
        $(target).hide().next().hide();
    });

    $(".ui-icon-trash").live("click", function() {
        $(target).show().next().show();
    })
}

function refreshValues(in1, in2, out) {
    var valor1 = $(in1).val();
    var valor2 = $(in2).val().trim() === "" ? 0 : $(in2).val();
    var retorno = valor1 * valor2;
    $(out).html("<p>" + retorno.toFixed(2) + "</p>");
}


function getAddress(cep) {

    $.ajax({
        type: "POST",
        url: "http://www.republicavirtual.com.br/web_cep.php",
        dataType: "jsonp",
        crossDomain: true,
        data: {
            formato: "javascript",
            cep: cep
        },
        beforeSend: function() {
            console.log("\nLoading\n");
        },
        complete: function() {
            console.log("\n\nO bairro de meu fi: " + resultadoCEP.bairro);
        }
    });
};

*/